<?php

######################################################################
# SmallNuke: Advanced Content Management System
# ============================================
# Copyright (c) 2004 by Sergey Vipolzov (info@smallnuke.com)
# http://smallnuke.com
# ============================================
# Developed from the PHP-Nuke 7.0
# Copyright (c) 2002 by Francisco Burzi
# http://phpnuke.org/
# ============================================
# This program is free software. You can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
######################################################################

if (!eregi("modules.php", $_SERVER['PHP_SELF'])) {
    die ("You can't access this file directly...");
}


require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
require_once("modules/$module_name/p_config.php");
get_lang($module_name);

function getparent($parentid,$title) {
    global $prefix,$db;
        $sql = "select cid, title, parentid from ".$prefix."_smallpages_categories where cid=$parentid";
        $res = $db->sql_query($sql);
        $row = $db->sql_fetchrow($res);
        $cid = $row[cid];
        $ptitle = $row[title];
        $pparentid = $row[parentid];
        mysql_free_result($result);
        if ($ptitle!="") $title=$ptitle." | ".$title;
        if ($pparentid!=0) {
        $title=getparent($pparentid,$title);
        }
        return $title;
}

function menu($c_cid) {
  global $prefix, $db, $module_name;
        
}

function footmenu() {
        global $module_name;
        
}

function links($sc_cid) {
        global $prefix, $db, $module_name;
        $sc_cid = intval($sc_cid);
        $sql = "SELECT cid  FROM ".$prefix."_smallpages_categories WHERE parentid = '$sc_cid'";
        $resnum = $db->sql_query($sql);
        if ($numrows = $db->sql_numrows($resnum) > 0) {
        $cat_links = "cat"; }
        else {
        $cat_links = "showcat"; }
        echo "<a style=\"TEXT-DECORATION: none\" href=\"modules.php?name=$module_name&go=$cat_links&cid=$sc_cid\">";
}

function main() {
    global $db, $prefix, $module_name, $multilingual, $currentlang, $tablecathome, $tablenumhome, $catdescrhome, $homesubcat, $newhome, $newhomenum, $shownumeric;
    include("header.php");
    OpenTable();
    echo "<center>"._PAGESNOTE."</center>";
    CloseTable();
    echo "<br>";
    OpenTable();
    if ($multilingual == 1) {
        $querylang = "AND (alanguage='$currentlang' OR alanguage='')";
    } else {
        $querylang = "";
    }
    if ($tablecathome == 1) {
    echo "<table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" width=\"100%\"><tr>";
    $tc = 0;
    $tdwidth = intval(100/$tablenumhome);
    }
    $sql = "SELECT cid, title, description FROM ".$prefix."_smallpages_categories WHERE parentid='0' $querylang";
    $result = $db->sql_query($sql);
    while ($row = $db->sql_fetchrow($result)) {
    $cid = $row[cid];
    $title = $row[title];
    $description = $row[description];
    if ($tablecathome == 1) { echo "<td valign=\"top\" width=\"".$tdwidth."%\">"; }
    echo "<img border=\"0\" src=\"modules/$module_name/images/cat_pic.gif\" width=\"10\" height=\"14\"> ";
    links($cid);
    echo "<b>$title</b></a>";
    if ($catdescrhome == 1) { echo "<br><i>$description</i>"; }
    if ($homesubcat == 1) {
    echo "<br>";
    $sc = 0;
    $sql2 = "SELECT cid, title FROM ".$prefix."_smallpages_categories WHERE parentid='$cid'";
    $result2 = $db->sql_query($sql2);
    while ($row = $db->sql_fetchrow($result2)) {
    $sc_cid = $row[cid];
    $sc_title = $row[title];
    if ($sc > 0) { echo ", "; }
    links($sc_cid);
    echo "$sc_title</a>";
    $sc++;
    }
    }
    if ($tablecathome == 1) {
    $tc++;
    if ($tc == $tablenumhome) { $tc = 0; echo "</td></tr><tr>"; }
    else { echo "</td>"; }
    }
    else { echo "<br><br>"; }
    }
    if ($tablecathome == 1) { echo "</table>"; }
    echo "<br><br>";
    if ($newhome == 1) {
    $sql3 = "SELECT pid, title, hometext, UNIX_TIMESTAMP(date) as postdate FROM ".$prefix."_smallpages WHERE cid !='0' AND active = '1' ORDER BY pid DESC LIMIT $newhomenum";
    $result3 = $db->sql_query($sql3);
    if ($numrows = $db->sql_numrows($result3) > 0) {
    echo "<b>"._NEWPAGES."</b><hr>";
    while ($row = $db->sql_fetchrow($result3)) {
    $p_pid = $row[pid];
    $p_title = $row[title];
    $p_hometext = $row[hometext];
    $p_hometext = stripslashes(check_html($p_hometext, nohtml));
    $p_date = date("d/m/Y", $row["postdate"]);
    echo "<img border=\"0\" src=\"modules/$module_name/images/page_pic.gif\" width=\"10\" height=\"14\"><b><a style=\"TEXT-DECORATION: none\" href=modules.php?name=$module_name&go=page&pid=$p_pid>$p_title</a></b><br><i>$p_hometext</i><br>"._SENDDATE.": $p_date<br><br>";
    }
    }
    }
    echo "<br><br>";
    $sql4 = "SELECT pid, title, hometext, UNIX_TIMESTAMP(date) as postdate FROM ".$prefix."_smallpages WHERE cid ='0' AND active = '1' ORDER BY pid DESC";
    $result4 = $db->sql_query($sql4);
    if ($numrows = $db->sql_numrows($result4) > 0) {
    echo "<b>"._PAGESHOMECATS."</b><hr>";
    while ($row = $db->sql_fetchrow($result4)) {
    $p_pid = $row[pid];
    $p_title = $row[title];
    $p_hometext = $row[hometext];
    $p_hometext = stripslashes(check_html($p_hometext, nohtml));
    $p_date = date("d/m/Y", $row["postdate"]);
    echo "<img border=\"0\" src=\"modules/$module_name/images/page_pic.gif\" width=\"10\" height=\"14\"><b><a style=\"TEXT-DECORATION: none\" href=modules.php?name=$module_name&go=page&pid=$p_pid>$p_title</a></b><br><i>$p_hometext</i><br>"._SENDDATE.": $p_date<br><br>";
    }
    }
    CloseTable();
    if ($shownumeric == 1) {
    echo "<br>";
    OpenTable();
    $num_cat = $db->sql_numrows($db->sql_query("SELECT cid FROM ".$prefix."_smallpages_categories"));
    $num_page = $db->sql_numrows($db->sql_query("SELECT pid FROM ".$prefix."_smallpages WHERE active='1'"));
    echo "<center>"._ALLP."<b>$num_page</b> "._ALLP2." <b>$num_cat</b> "._ALLP3."</center>";
    CloseTable();
    }

    include("footer.php");
}

function cat($cid) {
    global $db, $prefix, $module_name, $tablecat, $tablenum, $catdescr, $subcat;
    $cid = intval($cid);
    include("header.php");
    menu($cid);
    OpenTable();
    if ($tablecat == 1) {
    echo "<table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" width=\"100%\"><tr>";
    $tc = 0;
    $tdwidth = intval(100/$tablenum);
    }
    $sql = "SELECT cid, title, description, parentid FROM ".$prefix."_smallpages_categories WHERE parentid='$cid'";
    $result = $db->sql_query($sql);
    while ($row = $db->sql_fetchrow($result)) {
    $c_cid = $row[cid];
    $title = $row[title];
    $description = $row[description];
    $parentid = $row[parentid];
    if ($tablecat == 1) { echo "<td valign=\"top\" width=\"".$tdwidth."%\">"; }
    echo "<img border=\"0\" src=\"modules/$module_name/images/cat_pic.gif\" width=\"10\" height=\"14\"> ";
    links($c_cid);
    echo "<b>$title</b></a>";
    if ($catdescr == 1) { echo "<br><i>$description</i>"; }
    if ($subcat == 1) {
    echo "<br>";
    $sc = 0;
    $sql2 = "SELECT cid, title FROM ".$prefix."_smallpages_categories WHERE parentid='$c_cid'";
    $result2 = $db->sql_query($sql2);
    while ($row = $db->sql_fetchrow($result2)) {
    $sc_cid = $row[cid];
    $sc_title = $row[title];
    if ($sc > 0) { echo ", "; }
    links($sc_cid);
    echo "$sc_title</a>";
    $sc++;
    }
    }
    if ($tablecat == 1) {
    $tc++;
    if ($tc == $tablenum) { $tc = 0; echo "</td></tr><tr>"; }
    else { echo "</td>"; }
    }
    else { echo "<br><br>"; }
    }
    if ($tablecat == 1) { echo "</table>"; }
    echo "<br><br>";

    $sql4 = "SELECT pid, title, hometext, UNIX_TIMESTAMP(date) as postdate FROM ".$prefix."_smallpages WHERE cid ='$cid' AND active = '1' ORDER BY pid DESC";
    $result4 = $db->sql_query($sql4);
    if ($numrows = $db->sql_numrows($result4) > 0) {
    echo "<b>"._PAGESHOMECAT."</b><hr>";
    while ($row = $db->sql_fetchrow($result4)) {
    $p_pid = intval($row[pid]);
    $p_title = $row[title];
    $p_hometext = $row[hometext];
    $p_hometext = stripslashes(check_html($p_hometext, nohtml));
    $p_date = date("d/m/Y", $row["postdate"]);
    echo "<img border=\"0\" src=\"modules/$module_name/images/page_pic.gif\" width=\"10\" height=\"14\"><b><a style=\"TEXT-DECORATION: none\" href=modules.php?name=$module_name&go=page&pid=$p_pid>$p_title</a></b><br><i>$p_hometext</i><br>"._SENDDATE.": $p_date<br><br>";
    }
    }
    CloseTable();
    footmenu();
    include("footer.php");
}

function showcat($cid) {
    global $db, $prefix, $module_name, $numpage, $pagenum;
    $cid = intval($cid);
    include("header.php");
    menu($cid);
    OpenTable();
    if ($pagenum == "") {
    $pagenum = 1 ;
    }
    $offset = ($pagenum-1) * $numpage;

    $sql = "SELECT pid, title, hometext, UNIX_TIMESTAMP(date) as postdate, counter FROM ".$prefix."_smallpages WHERE cid ='$cid' AND active = '1' ORDER BY pid DESC limit $offset, $numpage";
    $result = $db->sql_query($sql);
    if ($numrows = $db->sql_numrows($result) > 0) {
    while ($row = $db->sql_fetchrow($result)) {
    $p_pid = intval($row[pid]);
    $p_title = $row[title];
    $p_hometext = $row[hometext];
    $p_hometext = stripslashes(check_html($p_hometext, nohtml));
    $p_date = date("d/m/Y", $row["postdate"]);
    $p_counter = $row[counter];
    echo "<img border=\"0\" src=\"modules/$module_name/images/page_pic.gif\" width=\"10\" height=\"14\">\n"
    ."<b><a style=\"TEXT-DECORATION: none\" href=modules.php?name=$module_name&go=page&pid=$p_pid>$p_title</a></b><br>\n"
    ."<i>$p_hometext</i><br>"._SENDDATE.": $p_date<br>"._COUNTPAGE.": $p_counter "._COUNT."<hr>";
    }
    }
    #START Page Numbers
    $sql_pn = "select pid from ".$prefix."_smallpages where  cid ='$cid' AND active = '1'";
    $result_pn = $db->sql_query($sql_pn);
    $numstories = $db->sql_numrows($result_pn);
    @$numpages = ceil($numstories / $numpage);
    if ($numpages > 1) {
        opentable();
        echo "<center>" ;
        if ($pagenum > 1) {
            $prevpage = $pagenum - 1 ;
                        $leftarrow = "images/left.gif" ;
                echo "<a href=\"modules.php?name=$module_name&amp;go=showcat&amp;cid=$cid&amp;pagenum=$prevpage\">";
                echo "<img src=\"$leftarrow\" align=\"absmiddle\" border=\"0\" hspace=\"10\"></a>";

        }
        for ($i=1; $i < $numpages+1; $i++) {

            if ($i == $pagenum) {
                echo "<b>$i</b>";
            }
            else {
            $pagelink = 10;
            if (($i > $pagenum) AND ($i < $pagenum+$pagelink) OR ($i < $pagenum) AND ($i > $pagenum-$pagelink)) {
            echo " <a href=\"modules.php?name=$module_name&amp;go=showcat&amp;cid=$cid&amp;pagenum=$i\">$i</a> ";
            }
            if (($i == $numpages) AND ($pagenum < $numpages-$pagelink)){
            echo "... <a href=\"modules.php?name=$module_name&amp;go=showcat&amp;cid=$cid&amp;pagenum=$i\">$i</a>";
            }
            if (($i == 1) AND ($pagenum > 1+$pagelink)){
            echo "<a href=\"modules.php?name=$module_name&amp;go=showcat&amp;cid=$cid&amp;pagenum=$i\">$i</a> ...";
            }
            }
        }
        if ($pagenum < $numpages) {
            $nextpage = $pagenum + 1 ;
                        $rightarrow = "images/right.gif" ;
                echo "<a href=\"modules.php?name=$module_name&amp;go=showcat&amp;cid=$cid&amp;pagenum=$nextpage\">";
                echo "<img src=\"$rightarrow\" align=\"absmiddle\" border=\"0\" hspace=\"10\"></a>";
        }
        echo "</center>" ;
        closetable();
    }
    #END Page Numbers
    CloseTable();
    footmenu();
    include("footer.php");
}

function page($pid, $page=0) {
    global $page_title, $hometext, $bodytext, $db, $prefix, $module_name, $admin, $pagecatlink, $pagecatnumlinc;
    $pid = intval($pid);
    if ($page == 0) {
    $db->sql_query("UPDATE ".$prefix."_smallpages SET counter=counter+1 WHERE pid='$pid'");
    }
    $sql = "SELECT cid, title, hometext, bodytext, UNIX_TIMESTAMP(date) as postdate, counter  FROM ".$prefix."_smallpages WHERE pid='$pid'";
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
    $cid = intval($row[cid]);
    $page_title = $row[title];
    $hometext = $row[hometext];
    $bodytext = $row[bodytext];
    $p_date = date("d/m/Y", $row["postdate"]);
    $counter = $row[counter];
    $contentpages = explode( "<!--pagebreak-->", $bodytext );
        $pageno = count($contentpages);
        if ( $page=="" || $page < 1 ) {
            $page = 1;
            }
        if ( $page > $pageno )
            $page = $pageno;
        $arrayelement = (int)$page;
        $arrayelement --;
    include("header.php");
    menu($cid);
    OpenTable();

    echo "<br>";
    echo "$contentpages[$arrayelement]";
       

    if (is_admin($admin)) {

if ($pageno != 1) {
echo "<hr>";
    }
        if($page >= $pageno) {
            $next_page = "";
        } else {
            $next_pagenumber = $page + 1;
            if ($page != 1) {
                $next_page .= "- ";
            }
            $next_page .= "<a style=\"TEXT-DECORATION: none\" href=\"modules.php?name=$module_name&go=page&pid=$pid&page=$next_pagenumber\"><b>"._NEXT."</b> ($next_pagenumber/$pageno)</a> <a href=\"modules.php?name=$module_name&go=page&pid=$pid&page=$next_pagenumber\"><img src=\"modules/$module_name/images/next_page.gif\" border=\"0\" alt=\""._NEXT."\" title=\""._NEXT."\"></a>";
        }
        if($page <= 1) {
            $previous_page = "";
        } else {
            $previous_pagenumber = $page - 1;
            $previous_page = "<a href=\"modules.php?name=$module_name&go=page&pid=$pid&page=$previous_pagenumber\"><img src=\"modules/$module_name/images/prev_page.gif\" border=\"0\" alt=\""._PREVIOUS."\" title=\""._PREVIOUS."\"></a> <a style=\"TEXT-DECORATION: none\" href=\"modules.php?name=$module_name&go=page&pid=$pid&page=$previous_pagenumber\"><b>"._PREVIOUS."</b> ($previous_pagenumber/$pageno)</a>";
        }
        echo "<center>$previous_page $next_page</center>";
        echo "<hr>"._SENDDATE.": $p_date<br>"._COUNTPAGE.": $counter "._COUNT.""; 

    echo "<hr><center><a href=admin.php?op=pages_delit_page&pid=$pid>"._DELPAGE."</a> | <a href=admin.php?op=pages_edit_page&pid=$pid>"._EDITPAGE."</a> | <a href=admin.php?op=pages_status_page&pid=$pid&act=off>"._OFFPAGE."</a></center>";
    }
    if ($pagecatlink == 1) {
    $sql2 = "SELECT pid, title FROM ".$prefix."_smallpages WHERE cid='$cid' AND pid !='$pid' AND active = '1' LIMIT $pagecatnumlinc";
    $result2 = $db->sql_query($sql2);
    $num = $db->sql_numrows($result2);
    if ($num > 0) {
    echo "<br><b>"._LINKPAGES."</b><hr>";
    while ($row = $db->sql_fetchrow($result2)) {
    $l_pid = $row[pid];
    $l_title = $row[title];
    echo "<img border=\"0\" src=\"modules/$module_name/images/page_pic.gif\" width=\"10\" height=\"14\"> \n"
    ."<a style=\"TEXT-DECORATION: none\" href=modules.php?name=$module_name&go=page&pid=$l_pid><b>$l_title</b></a><br>";
    }
    }
    }
    CloseTable();
    footmenu();
    include("footer.php");
}


 switch($go) {

    case "cat":
    cat($cid);
    break;

    case "showcat":
    showcat($cid);
    break;

    case "page":
    page($pid, $page);
    break;

    default:
    main();
    break;

}


?>